const BASE_URL = getApp().globalData.baseUrl

const baseRequest = async (url, method, data = {}, loading = true) => {
	let header = {}
	header.hToken = uni.getStorageSync('Token');
	return new Promise((reslove, reject) => {
		uni.request({
			url: BASE_URL + url,
			method: method || 'GET',
			header: header,
			timeout: 10000,
			data: data || {},
			success: (successData) => {
				const body = successData.data
				// console.log(body.code)
				if (body.code == 200) {
					// console.log('code 200')
					reslove(body)
				} else if (body.code == 401) {
					uni.showToast({
						icon: 'none',
						title: body.msg
					});
					getApp().exit();
					reject(body)
				} else {
					// console.log('code else')
					uni.showToast({
						icon: 'none',
						title: body.msg
					});
					reject(body)
				}

			},
			fail: (msg) => {
				uni.showToast({
					icon: 'none',
					title: '网络连接失败，请稍后重试'
				});
				reject(msg)
			}
		})
	})
}

const request = {}
request.post = (api, data) => baseRequest(api, "POST", data);
request.get = (api, data) => baseRequest(api, "GET", data);

export default request